﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WisDom.Models;

namespace WisDom.DBContext
{
    /// <summary>
    /// 
    /// </summary>
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }
        public DbSet<AuditEntity> AuditEntity { get; set; }

        public DbSet<BackupClientFileEntity> BackupClientFileEntitys { get; set; }

        public DbSet<PackageInfoEntity> PackageInfoEntitys { get; set; }

        public DbSet<PackageSetupInfoEntity> PackageSetupInfoEntitys { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AuditEntity>(entity =>
            {
                entity.ToTable("UP_Audit");
                entity.HasKey(n => n.Id);
                entity.Property(n => n.Id).ValueGeneratedOnAdd();
                entity.Property(n => n.TableName).HasColumnType("varchar(50)");
                entity.Property(n => n.OperTime).HasColumnType("DateTime");
                entity.Property(n => n.KeyValues).HasColumnType("varchar(50)");
                entity.Property(n => n.NewValues).HasColumnType("text");
                entity.Property(n => n.OldValues).HasColumnType("text");
                entity.Property(n => n.OperationString).HasColumnType("varchar(50)");
            });
            modelBuilder.Entity<BackupClientFileEntity>(entity =>
            {
                entity.ToTable("UP_BackupClientFile");
                entity.HasKey(n => n.FileId);
                entity.Property(n => n.FileId).ValueGeneratedOnAdd();
                entity.Property(n => n.FileName).HasColumnType("varchar(50)");
                entity.Property(n => n.ClientIp).HasColumnType("varchar(50)");
                entity.Property(n => n.BackupTypeString).HasColumnType("varchar(50)");
                entity.Property(n => n.FilePath).HasColumnType("text");
                entity.Property(n => n.Remark).HasColumnType("text");
                entity.Property(n => n.CreateDateTime).HasColumnType("DateTime"); 
                entity.Property(n => n.ModifyDateTime).HasColumnType("DateTime"); 
            });
            modelBuilder.Entity<PackageInfoEntity>(entity =>
            {
                entity.ToTable("UP_PackageInfo");
                entity.HasKey(n => n.PackageId);
                entity.Property(n => n.PackageId).ValueGeneratedOnAdd();
                entity.Property(n => n.PackageName).HasColumnType("varchar(50)");
                entity.Property(n => n.UpdatePath).HasColumnType("varchar(500)");
                entity.Property(n => n.CheckVersionFile).HasColumnType("varchar(50)");
                entity.Property(n => n.PackageTypeString).HasColumnType("varchar(50)");
                entity.Property(n => n.CreateDateTime).HasColumnType("DateTime");
                entity.Property(n => n.ModifyDateTime).HasColumnType("DateTime"); 
            });
            //包建立信息
            modelBuilder.Entity<PackageSetupInfoEntity>(entity =>
            {
                entity.ToTable("UP_PackageSetupInfo");
                entity.HasKey(n => n.SetupId);
                entity.Property(n => n.SetupId).ValueGeneratedOnAdd();
                entity.Property(n => n.PackageId).HasColumnType("int");
                entity.Property(n => n.Version).HasColumnType("varchar(50)");
                entity.Property(n => n.SavePath).HasColumnType("text");
                entity.Property(n => n.IsGetLast).HasColumnType("bit");
                entity.Property(n => n.IsZip).HasColumnType("bit");
                entity.Property(n => n.CreateDateTime).HasColumnType("DateTime"); 
                entity.Property(n => n.ModifyDateTime).HasColumnType("DateTime"); 
            });
            foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {

                foreach (var item in entity.GetProperties().Where(n=>n.Name.ToLower().IndexOf("create")>=0|| n.Name.ToLower().IndexOf("modify")>=0))
                {
                    if(item.ClrType==typeof(string))
                    {
                        item.SetMaxLength(50);
                    }
                }
            }
            base.OnModelCreating(modelBuilder);
        }
    }
}
